import requests
import parsel
# https://fanqienovel.com/
# 模拟浏览器对于url地址发送请求
# 模拟浏览器
# 1 开发者工具中的请求表头 字典格式
headers = {
    # 用户信息，常用于检测是否有登录账号
    "Cookie":"Hm_lvt_2667d29c8e792e6fa9182c20a3013175=1723307718; HMACCOUNT=BFA092251131D83D; s_v_web_id=verify_lzocx87p_QTG0ZDVF_yPsZ_43LK_8Akf_EpTqlQRHIbN4; csrf_session_id=96d87a1f361f1dac97aeb6226ec7ed09; novel_web_id=7401550230586099209; Hm_lpvt_2667d29c8e792e6fa9182c20a3013175=1723309868; ttwid=1%7Cg2MZEXjMqknRvGKbmQDPjyEYw4sspPp3o1TX3_0Lz7M%7C1723309869%7C7fa1b6071058e2ccb1dbff2e809b5cf27f906e055ab8816e12ebb64d9e6745ca",
    # 用户代理
    "user-agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36",
}
url = "https://fanqienovel.com/page/7348370753328253977"
selector = None
m = {
    12304: "丁",
    20041: "知"

}
# 获取响应内容
def get_html(url):
    resp = requests.get(url=url,headers=headers)
    resp.encoding = "utf-8"
    return resp.text
# 解析网页内容
def get_content(html):
    pattern = '&nbsp;&nbsp;&nbsp;&nbsp;.*<br/><br/>'


if __name__ == '__main__':
    # 把html字符串数据转成可解析的对象
    html = get_html(url)
    selector = parsel.Selector(html)
    # 提取标题（书名）
    # 获取第一个里面的内容（匹配到多个也是取第一个）
    book_name = selector.css(".info-name h1::text").get()
    # 获取章节名字
    title_lst = selector.css(".chapter-item-title::text").getall()
    # 获取章节id (获取标签属性)
    id_lst = selector.css(".chapter-item .chapter-item-title::attr(href)").getall()
    # 打包数据
    # title_link_list = []
    for title, link in zip(title_lst, id_lst):
        # title_link_list.append([title, link])
        # 完整的链接
        link_url = "https://fanqienovel.com" + link
        link_data = requests.get(url=link_url, headers=headers).text
        # 提取小说内容
        link_selector = parsel.Selector(link_data)
        content_lst = link_selector.css("div p::text").getall()
        content = "\n\n".join(content_lst)
        for item in content:
            # 根据码表自己替换（我这里做下测试的）
            print(item, ord(item), m.get(ord(item)) )
        break
    # print(title_link_list)